buildDataObject <- function(searchTableLoc='./prodAlgoTable.csv',dataObjectOutputLoc='./searchData.RData'){
  
  require(XML)
  require(stringr)
  
  prodAlgoTable <- read.csv(searchTableLoc,quote="",stringsAsFactors=FALSE)
  
  searchData <- NULL
  for(r in 1:nrow(prodAlgoTable)){
    
    thisProdRow <- prodAlgoTable[r,]
    searchSummary <- xmlParse(str_c('http://api.indeed.com/ads/apisearch?',
                            'publisher=2487201386265292&',
                            'q=',thisProdRow$algo,'&limit=1&v=2'))
    
    totalCount <- as.numeric(xmlToDataFrame(getNodeSet(searchSummary, "//totalresults"),
                                            stringsAsFactors=FALSE)$text)
    
    totalPages <- ceiling(totalCount/25)
    
    cat(str_c('\n\nPROCESSING ', totalCount,' RESULTS FOR  ', thisProdRow$productCode,
              '\n\nQ=',thisProdRow$algo,'\n\n'))
    progressBar <- txtProgressBar(max=totalPages,style=3)
    
    thisAlgoData_raw <- NULL
    for (p in 1:totalPages){
      
      start_i <- (p-1)*25 
      
      page_p <- str_c('http://api.indeed.com/ads/apisearch?',
                      'publisher=2487201386265292&',
                      'q=',thisProdRow$algo,'&limit=25&start=',start_i,'&v=2')
      
      results <- xmlParse(page_p)
      data <- xmlToDataFrame(getNodeSet(results, "//result"),stringsAsFactors=FALSE)
   
      #thisAlgoData <- rbind(thisAlgoData,data)
      thisAlgoData_raw <- rbind(thisAlgoData_raw,data.frame(
                                           vendorCode=thisProdRow$vendorCode,
                                           productCode=thisProdRow$productCode,
                                           domainCode=thisProdRow$domainCode,
                                           algoCode=thisProdRow$algoCode,
                                           company=data$company,
                                           jobkey=data$jobkey,
                                           loc=data$formattedLocation,
                                           postSource=data$source,stringsAsFactors=FALSE))
      
      setTxtProgressBar(progressBar,p)
          
    }
  
    thisAlgoData_u <- unique(thisAlgoData_raw)  
    cat(str_c('\n\n ... ', nrow(thisAlgoData_u),' of ', nrow(thisAlgoData_raw), ' are unique.'))
    thisAlgoData <- thisAlgoData_u[!str_trim(thisAlgoData_u$company)=='',]
    cat(str_c('\n\n ... removing ', sum(str_trim(thisAlgoData_u$company)==''),' rows with no company name.'))
    
    thisAlgoData_u$rank <- 1:nrow(thisAlgoData_u)
    
    searchData <- rbind(searchData,thisAlgoData)
    save(searchData,file=dataObjectOutputLoc)                      
  }

}

quickReport.byProduct <- function(dataInputObjectLoc='./searchData.RData',outputChartLoc='./outputCharts.html',numOfCompanies=20){
  
  require(plyr)
  require(googleVis)
  require(stringr)
  
  load(dataInputObjectLoc)
  
  prodCompSum <- dlply(searchData,c('productCode'),function(thisProd){
    compLoc <- ddply(thisProd,'company',function(thisComp){
      return(data.frame(uniqueLocations=length(unique(thisComp$loc))))
    })
    compTable <- as.data.frame(table(thisProd$company))
    compTable_topN <- compTable[order(compTable$Freq,decreasing=TRUE),][1:numOfCompanies,]
    names(compTable_topN) <- c('company','productHits')  
    output <- merge(compTable_topN,compLoc,by='company')
    output <- output[order(output$productHits,decreasing=TRUE),]
  })
  
  allCharts <- list()
  for(pr in 1:length(prodCompSum)){
    titleStr <- str_c('Top ', numOfCompanies, ' Companies Using ',names(prodCompSum)[pr])
    allCharts[[pr]] <- gvisBarChart(prodCompSum[[pr]],options=list(
      title=titleStr,legend='top', width=1000, height=900))
  }
  output <- allCharts[[1]]
  for (tc in 2:length(allCharts)) output <- gvisMerge(output,allCharts[[tc]])
  capture.output(print(output),file=outputChartLoc)
  plot(output)

}


# converts text that would normally go in the indeed.com search bar to a 
# formatted URL query to be used with q=<functionOutput>
indeed.buildQuery<- function(searchBarText){
  str1 <- str_replace_all(searchBarText,'\\(','%28')
  str2 <- str_replace_all(str1,'\\)','%29')
  str3 <- str_replace_all(str2,' ','+')
  str4 <- str_replace_all(str3,'\"','%22')
  str5 <- str_replace_all(str4,':','%3A')
  return(str5)
}
